﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;

using ERPStore.Web.Extensions;
using System.Web.Optimization;

using CloudSoft.Extensions;

namespace ERPStore.SearchOptimization
{
	public class SearchOptimizationService : ERPStore.Web.Services.IOptimizationService
	{
		public SearchOptimizationService()
		{
		}

		public virtual void GenerateMetasInformations(System.Web.HttpContextBase context, object model)
		{
			var currentRoute = RouteTable.Routes.GetRouteData(context);
			var controller = currentRoute.Values["Controller"];
			var action = currentRoute.Values["Action"];
			var language = currentRoute.Values["Language"];
			var uniqueName = string.Format("{0}/{1}", controller, action);
			string h1 = null;
			string title = null;
			string description = null;
			Models.ProductCategory currentCategory = null;
			Models.Brand currentBrand = null;

			var sort = string.Empty;
			var sortBy = context.Request["sortby"];
			if (sortBy != null)
			{
				sort = "trié par ";
				switch (sortBy.ToLower())
				{
					case "title":
						sort += "nom";
						break;
					case "saleprice":
						sort += "prix";
						break;
					case "stats.ordercount":
						sort += "meilleures ventes";
						break;
				}
				if (context.Request["sortdirection"] == "desc")
				{
					sort += " dans l'ordre décroissant";
				}
			}


			switch (uniqueName.ToLower())
			{
				case "catalog/destock":
					h1 = string.Format("Le destockage");
					break;
				case "catalog/topsells":
					h1 = string.Format("Top ventes");
					break;
				case "catalog/promotions":
					h1 = string.Format("Les promotions");
					break;
				case "catalog/newproducts":
					h1 = string.Format("Les nouveautés produit");
					break;
				case "catalog/productcategories":
				case "catalog/categorylist":
					h1 = string.Format("Les categories de produit du catalogue {0}", GlobalConfiguration.Configuration.Settings.SiteName);
					break;
				case "catalog/productbycategory":
				case "catalog/category":
					var categoryList = model as ERPStore.Models.ProductList;
					if (categoryList != null)
					{
						title = string.Format("{0} page {1}/{2}", categoryList.Category.PageTitle, categoryList.PageIndex, Math.Ceiling(categoryList.ItemCount / (categoryList.PageSize * 1.0)));
						h1 = string.Format("Les categories de produit du catalogue {0}", GlobalConfiguration.Configuration.Settings.SiteName);
						description = categoryList.Category.PageDescription ?? categoryList.Category.Name;
						currentCategory = categoryList.Category;
						currentBrand = categoryList.Brand;
					}
					break;
				case "catalog/productbybrand":
				case "catalog/brand":
					var productByBrandList = model as ERPStore.Models.ProductList;
					h1 = string.Format("Les marques du catalogue {0}", GlobalConfiguration.Configuration.Settings.SiteName);
					if (productByBrandList != null)
					{
						currentCategory = productByBrandList.Category;
						currentBrand = productByBrandList.Brand;
					}
					break;
				case "catalog/brands":
				case "catalog/brandlist":
					h1 = string.Format("Les marques du catalogue {0}", GlobalConfiguration.Configuration.Settings.SiteName);
					break;
				case "catalog/search":
					var searchList = model as ERPStore.Models.ProductList;
					if (searchList != null)
					{
						h1 = string.Format("Résultat de recherche dans le catalogue produit pour {0} page {1}/{2}", searchList.Query, searchList.PageIndex, Math.Ceiling(searchList.ItemCount / (searchList.PageSize * 1.0)));
					}
					break;
				case "catalog/product":
					var product = model as ERPStore.Models.Product;
					if (product != null)
					{
						h1 = string.Format("Le catalogue produit {0}", GlobalConfiguration.Configuration.Settings.SiteName);
						currentCategory = product.Category;
						currentBrand = product.Brand;
					}
					break;
				case "home/index":
				case "home/catchall":
				default:
					break;
			}
			if (!h1.IsNullOrTrimmedEmpty())
			{
				context.Items["OptimizedH1"] = h1;
			}
			if (!title.IsNullOrTrimmedEmpty())
			{
				context.Items["OptimizedTitle"] = title;
			}
			if (!description.IsNullOrTrimmedEmpty())
			{
				context.Items["OptimizedDescription"] = description;
			}
			if (currentCategory != null)
			{
				context.Items["currentCategory"] = currentCategory;
			}
			if (currentBrand != null)
			{
				context.Items["currentBrand"] = currentBrand;
			}
		}

		public virtual void RegisterBundles(System.Web.HttpContextBase context)
		{
			var bundleFolder = context.Server.MapPath("~/bundle");
			BundleTable.Bundles.UseCdn = true;
			if (System.IO.Directory.Exists(bundleFolder))
			{
				// Bundlelization
				var jsBundle = new Bundle("~/bundle/js", new JsMinify());
				jsBundle.IncludeDirectory("~/bundle", "*.js", false);
				BundleTable.Bundles.Add(jsBundle);

				var cssBundle = new Bundle("~/bundle/css", new CssMinify());
				cssBundle.IncludeDirectory("~/bundle", "*.css", false);
				BundleTable.Bundles.Add(cssBundle);
			}

			var jqueryBundle = new ScriptBundle("~/bundle/jquery",
				"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")
				.Include("~/Scripts/jquery-{version}.js");
			BundleTable.Bundles.Add(jqueryBundle);

			var jqueryUIBundle = new ScriptBundle("~/bundle/jqueryui",
				"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js")
				.Include("~/Scripts/jquery-ui-{version}.js");
			BundleTable.Bundles.Add(jqueryUIBundle);

			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/jqueryval").Include(
						"~/Scripts/jquery.unobtrusive*",
						"~/Scripts/jquery.validate*"));

			// Use the development version of Modernizr to develop with and learn from. Then, when you're
			// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/modernizr").Include(
						"~/Scripts/modernizr-{version}.js"));

			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/knockout").Include(
						"~/Scripts/knockout-{version}.js"));

			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/signalr").Include(
						"~/Scripts/jquery.signalR-{version}.js"));

			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/erpstore").Include(
						"~/Scripts/erpstore.*"));

			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/angularjs").Include(
				"~/scripts/angular.js"
				, "~/scripts/angular-loader.js"
				, "~/scripts/angular-bootstrap.js"
				, "~/scripts/angular-scenario.js"
				, "~/scripts/angular-sanitize.js"
				, "~/scripts/angular-resource.js"
				, "~/scripts/angular-cookies.js"
				, "~/scripts/angular-bootstrap-prettify.js"));

			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/ui-angularjs").Include(
				"~/scripts/ui-bootstrap-{version}.js"
			));

			BundleTable.Bundles.Add(new ScriptBundle("~/bundle/bootstrapjs").Include(
				"~/scripts/bootstrap.js"));

			BundleTable.Bundles.Add(new StyleBundle("~/bundle/bootstrap").Include(
				"~/content/bootstrap.css"
				, "~/content/bootstrap-responsive.css"));
		}

	}
}